﻿using KJSyncXP.Extensions;
using KJSyncXP.Sqlite.Model;
using KJSyncXP.ViewModels;
using System.Windows;
using System.Windows.Controls;

namespace KJSyncXP.Views
{
    /// <summary>
    /// UserPage.xaml 的交互逻辑
    /// </summary>
    /// <summary>
    /// UserPage.xaml 的交互逻辑
    /// </summary>
    public partial class UserPage : UserControl
    {
        UserManagerVM _authorizationVM;

        public UserPage(UserManagerVM authorizationVM)
        {
            InitializeComponent();
            _authorizationVM = authorizationVM;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            _authorizationVM.init();
            this.DataContext = _authorizationVM;
        }

        private void lvUserList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                var user = e.AddedItems[0] as User;
                _authorizationVM.CurrentUser = user;
                _authorizationVM.refreshUp();
            }
        }

        private void lvPermissionList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //if (!AuthenticationManager.Instance.authorize("UserPwdModify")) return;
            ListView lv = sender as ListView;
            var p = lv.SelectedItem as Permission;
            if (p == null) return;
            _authorizationVM.AddUp(p.Pname, p.Puuid,p.Ptype);
            _authorizationVM.refreshUp();
        }

        private void lvPermissionList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                var p = e.AddedItems[0] as Permission;
                _authorizationVM.CurrentPermission = p;
            }
        }

        private void lvUpList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //if (!AuthenticationManager.Instance.authorize("UserPwdModify")) return;
            ListView lv = sender as ListView;
            var p = lv.SelectedItem as Up;
            if (p == null) return;
            if (MessageBox.Show("移除访问令牌？", "应用提示", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
            {
                _authorizationVM.removeUp(p.Puuid);
                _authorizationVM.refreshUp();
            }
        }

        private void lvUserList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //if (!AuthenticationManager.Instance.authorize("UserPwdModify")) return;
            if (_authorizationVM.CurrentUser == null) return;
            UserWin win = new UserWin();
            win.Title = "修改用户信息";
            if (win.ShowDialog().Value)
            {
                _authorizationVM.saveUser();
                _authorizationVM.init();
            }
        }

        private void UserCreate_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;// AuthenticationManager.Instance().authorize("UserCreate");
        }

        private void UserDelete_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = AuthenticationManager.Instance.authorize("UserDelete");
        }

        private void UserPwdModify_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = AuthenticationManager.Instance.authorize("UserPwdModify");
        }

        private void UserCreate_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            UserWin win = new UserWin();
            win.Title = "创建新用户";
            _authorizationVM.newUser();
            if (win.ShowDialog().Value)
            {
                _authorizationVM.saveUser();
                _authorizationVM.init();
            }
        }

        private void UserDelete_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            if (_authorizationVM.CurrentUser == null) return;

            if (MessageBox.Show($"确要删除当前所选\"{_authorizationVM.CurrentUser.Username}\"用户？", "应用提示", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
            {
                _authorizationVM.removeUser();
                _authorizationVM.init();
            }
        }

        private void UserPwdModify_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            lvUserList_MouseDoubleClick(null, null);
        }
    }
}
